package org.example.config;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zhenzi.sms.ZhenziSmsClient;

import java.util.HashMap;
import java.util.Map;

/**
 * author: 李国树
 * date: 2022/8/30
 * desc: 榛子云短信
 */
public class ZzyUtils {
    public static final String APP_ID="113655";
    public static final String APP_SECRET="ODc4MmJmY2QtMDFjYi00Njc5LTljMmItZGUxMjM4MDQ1N2U0";
    public static final String API_URL ="https://sms_developer.zhenzikj.com";
    /**
     * 榛子云短信通知模版
     */
    public static final String TEMPLATE_ID="12499";
    public static boolean sendSms(String phone,String msg){
        try {
            ZhenziSmsClient client = new ZhenziSmsClient(API_URL, APP_ID, APP_SECRET);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("number", phone);//电话号码
            params.put("templateId", TEMPLATE_ID);//短信模板
            String[] templateParams = new String[1];
            templateParams[0] = msg;
            params.put("templateParams", templateParams);
            String result = client.send(params);//发送短信
            System.out.println("pppp==="+result);
            JSONObject jsonObject = JSONUtil.parseObj(result);
            System.out.println("www==="+jsonObject);
            Object code = jsonObject.get("code");
            if ("0".equals(code + "")) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }
}